package com.javaee.web.servlet;
import java.io.IOException;
import java.util.*;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javaee.ejb.entity.Conversation;
import com.javaee.ejb.session.remote.ConversationManagerRemote;
import com.javaee.web.HttpServletBase;
import com.javaee.web.SiteParameters;

/**
 * Servlet implementation class ConversationServlet
 */
public class ConversationServlet extends HttpServletBase {
	private static final long serialVersionUID = 1L;

	@EJB
	private ConversationManagerRemote conversationManager;

	/**
	 * @see HttpServletBase#HttpServletBase()
	 */
	public ConversationServlet() {
		super();
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		List<Conversation> conversationListTemp = conversationManager
				.getListByEmployee(currentEmployee.getId());
		List<Conversation> conversationEnCoursList = new ArrayList<Conversation>();
		List<Conversation> conversationRecueList = new ArrayList<Conversation>();
		List<Conversation> conversationEnvoyeeList = new ArrayList<Conversation>();
		List<Conversation> conversationEndedList = new ArrayList<Conversation>();

		for (Conversation conversation : conversationListTemp) {
			if (conversation.getStatus() == 1) {
				conversationEnCoursList.add(conversation);
			} else if (conversation.getStatus() > 1) {
				conversationEndedList.add(conversation);
			} else if (currentEmployee.getId() == conversation.getCalled()
					.getId() && conversation.getStatus() == 0) {
				conversationRecueList.add(conversation);
			} else if (currentEmployee.getId() == conversation.getCaller()
					.getId() && conversation.getStatus() == 0) {
				conversationEnvoyeeList.add(conversation);
			}
		}

		request.setAttribute("conversationEnCoursList", conversationEnCoursList);
		request.setAttribute("conversationRecueList", conversationRecueList);
		request.setAttribute("conversationEnvoyeeList", conversationEnvoyeeList);
		request.setAttribute("conversationEndedList", conversationEndedList);

		loadJSP(SiteParameters.getPagePath("conversation"), request, response);
	}
}
